package com.itheima.service.impl;

import com.itheima.domain.User;
import com.itheima.proxy.ServiceJDKProxy;
import com.itheima.service.UserService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

//@Service  目标类
public class UserServiceImpl implements UserService {

    public UserServiceImpl(){
        System.out.println("new 对象");
    }
    @Override
    public void save(User user) {
        //long start = System.currentTimeMillis();

        // ==========业务调用，加睡眠是模拟业务调用的耗时
        try {
            Thread.sleep((int)(Math.random()*1000));
            System.out.println("保存了用户数据~");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //===========

//        System.out.println("当前方法执行前初始时间毫秒值是:"+start);
//        System.out.println("当前方法执行后耗时:"+(System.currentTimeMillis()-start));
  }
    @Override
    public List<User> findAll() {
//        long start = System.currentTimeMillis();
        // =====业务调用，加睡眠是模拟业务调用的耗时
        try {
            Thread.sleep((int)(Math.random()*1000));
            System.out.println("查询了用户数据~");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // ====
//        System.out.println("当前方法执行前初始时间毫秒值是:"+start);
//        System.out.println("当前方法执行后耗时:"+(System.currentTimeMillis()-start));
        return new ArrayList<>();
    }
    @Override
    public void update(User user) {

        // ==========业务调用，加睡眠是模拟业务调用的耗时
        try {
            // 模拟业务调用耗时
            Thread.sleep((int)(Math.random()*1000));
            System.out.println("更新了用户数据~");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // =====
    }
    @Override
    public void delete(String id) {
        long start = System.currentTimeMillis();
        // =====业务调用，加睡眠是模拟业务调用的耗时
        try {
            Thread.sleep((int)(Math.random()*1000));
            System.out.println("删除了用户数据~");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // ====
        System.out.println("当前方法执行前初始时间毫秒值是:"+start);
        System.out.println("当前方法执行后耗时:"+(System.currentTimeMillis()-start));
    }
}
